<?php
namespace app\cms\controller;
use framework\ext\Check;
/* *
 * 资源碎片
 * */
class FragmentController extends \app\base\controller\AdminController{

    //首页
    public function index(){
        $act = form('get.act');
        if($act == 'del'){
            $condition['id'] = form('get.id',0,intval);
            $result =  model('fragment')->_delete($condition);
            if($result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg('删除失败');
            }
        }else{
            $page = form('get.page',0,intval);
            $this->list = model('fragment')->pager($page,20)->_select($this->website_id);
            $this->pager = getPage(model('fragment')->pager);
            $this->display();
        }
    }

    //添加
    public function edit(){
        if($this->isPost()){
            $msg = Check::rule(
                array(check::must($_POST['title']),'碎片名称必须填写'),
                array(check::must($_POST['about']),'碎片简述必须填写'));
            if(true !== $msg){
                $this->pageMsg($msg);
            }
            $data = array();
            $data = form('post',array('fid'));
            $data['uptime'] = time();
            $data['website_id'] = $this->website_id?$this->website_id:form('post.website_id',0,intval);
            $fid = form('post.fid',0,intval);
            if($fid){
                $condition['id'] = $fid;
                $rel = model('fragment')->_update($data,$condition);
            }else{
                $rel = model('fragment')->_update($data);
            }
            if($rel){
                $this->redirect(url('fragment/index'));
            }else{
                $this->pageMsg('碎片设置失败');
            }
        }else{
            $this->fid = form('get.id',0,intval);
            $this->info = model('fragment')->_find(array('id' =>$this->fid));
            $this->display();
        } 
    }
}